// NotesActivity.java
package com.example.sqltowebdemo;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.util.ArrayList;
import java.util.List;

public class NotesActivity extends AppCompatActivity {
    private ListView listViewNotes;
    private ApiService apiService;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes);

        apiService = RetrofitClient.getClient().create(ApiService.class);
        username = getIntent().getStringExtra("username");

        listViewNotes = findViewById(R.id.listViewNotes);

        Call<List<Note>> call = apiService.getNotes(username);
        call.enqueue(new Callback<List<Note>>() {
            @Override
            public void onResponse(Call<List<Note>> call, Response<List<Note>> response) {
                if (response.isSuccessful()) {
                    List<Note> notesList = response.body();
                    ArrayList<String> notesTextList = new ArrayList<>();
                    for (Note note : notesList) {
                        notesTextList.add(note.getText());
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(NotesActivity.this, android.R.layout.simple_list_item_1, notesTextList);
                    listViewNotes.setAdapter(adapter);
                } else {
                    Toast.makeText(NotesActivity.this, "Failed to Fetch Notes", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Note>> call, Throwable t) {
                Toast.makeText(NotesActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}